
import loader from '@/logic/loader';
import {App} from '@/logic/runtimeManager/App';
import {Page} from '@/logic/runtimeManager/Page';
import navigation from '@/logic/Navigation';

class RuntimeManager {
    constructor() {
        this.app = null;
        this.pages={}
    }

    createApp(opts) {
        
        const {pagePath,query,scene}=opts
        const appModuleInfo=loader.staticModules.app.moduleInfo;
        if(this.app){
            return false;
        }
        this.app = new App(appModuleInfo,{pagePath,query,scene});
    }

    createPage(opts) {
        console.log("createPage2")
        const {id,bridgeId,path,query}=opts;
        const staticModules=loader.getModuleByPath(path);

        navigation.pushState({ bridgeId,pagePath:path,query })
        this.pages[id]=new Page(staticModules,{id,bridgeId,path,query})
    }

    appShow() {
        this.app.callShowLifecycle()
    }

    appHide() {
        this.app.onHide()
    }
    pageShow(opts) {
        const { id }=opts;
        const page=this.pages[id];

        page.onShow && page.onShow();
    }

    pageHide(opts) {
        const { id }=opts;
        const page=this.pages[id];

        page.onHide && page.onHide();
    }
    pageUnload(opts) {
        const { id }=opts;
        const page=this.pages[id];
        navigation.popState();
        page.onUnload && page.onUnload();
        delete this.pages[id];
    }
    pageReady(opts) {
        const { id }=opts;
        const page=this.pages[id];

        page.onReady && page.onReady();

    }
    pageScroll(opts) {
        const { id,scrollTop }=opts;
        const page=this.pages[id];

        page.onPageScroll && page.onPageScroll(scrollTop);

    }
    trrigerEvent(opts) {
        const { id,methodName,param }=opts;
        var page=this.pages[id];

        if(typeof methodName==='string'){
            page[methodName] && page[methodName](param);
        }else{
            page[methodName.methodName] && page[methodName.methodName](param);
        }
    }

}

export default new RuntimeManager();